package com.micangcaihang.micang.mvp.login.ui;

import android.os.Bundle;
import android.view.View;

import com.micangcaihang.common.base.activity.BaseActivity;
import com.micangcaihang.common.utils.BarUtils;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.common.utils.avalidations.EditTextValidator;
import com.micangcaihang.common.utils.avalidations.ValidationModel;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.ActivityLoginBinding;
import com.micangcaihang.micang.entity.UserEntity;
import com.micangcaihang.micang.entity.UserInfoSp;
import com.micangcaihang.micang.mvp.login.contract.LoginContract;
import com.micangcaihang.micang.mvp.login.presenter.LoginPresenter;
import com.micangcaihang.micang.mvp.main.ui.DMainActivity;
import com.micangcaihang.micang.mvp.main.ui.FMainActivity;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/08/02
 *     update : 2017/08/02
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class LoginActivity extends BaseActivity<LoginPresenter, ActivityLoginBinding> implements LoginContract.View {
    private EditTextValidator mLoginValidator;

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        super.initView();
        mViewBinding.setClickHadnler(new ClickHandler());
        BarUtils.setStatusBarAlpha(LoginActivity.this, 0);
        BarUtils.addMarginTopEqualStatusBarHeight(mViewBinding.ivLogo);
    }

    /**
     * 获取传递的bundle数据
     *
     * @param extras
     */
    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void initData() {
        super.initData();
        mLoginValidator = new EditTextValidator(this)
                .add(new ValidationModel(mViewBinding.etUsername, 1, null))
                .add(new ValidationModel(mViewBinding.etPwd, 1, null))
                .setButton(mViewBinding.btnLogin)
                .execute();
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {

    }

    /**
     * 返回登录结果用户数据
     *
     * @param userEntity
     */
    @Override
    public void returnLoginData(UserEntity userEntity) {
        UserInfoSp.getInstance().setAccess(userEntity);
        if (userEntity.getDepartment_id() == 18) {
            startActivity(FMainActivity.class);
            finish();
        } else if (userEntity.getDepartment_id() == 19) {
            startActivity(DMainActivity.class);
            finish();
        } else {
            ToastUtils.showLong("您暂时没有权限登录");
        }
    }

    public class ClickHandler {
        public void onLogin(View view) {
            mPresenter.httpLogin(mViewBinding.etUsername.getText().toString(),
                    mViewBinding.etPwd.getText().toString());
        }
    }
}
